package com.nsoft.thickclient.trackMove.menu;

import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

import com.nsoft.thickclient.controller.ApplicationContest;
import com.nsoft.thickclient.controller.ApplicationController;
import com.nsoft.thickclient.menu.NMenuItem;
import com.nsoft.thickclient.trackMove.account.AccountViewController;
import com.nsoft.thickclient.trackMove.gc.GcViewController;
import com.nsoft.thickclient.trackMove.intr.user.User;
import com.nsoft.thickclient.trackMove.party.PartyViewController;
import com.nsoft.thickclient.trackMove.search.SearchToolViewController;
import com.nsoft.thickclient.trackMove.user.LoginDialog;
import com.nsoft.thickclient.trackMove.user.LoginViewController;
import com.nsoft.thickclient.trackMove.vehicle.VehicleViewController;
import com.nsoft.thickclient.utils.IconFactory;
import com.nsoft.thickclient.utils.MessageUtilities;

public class TrackMoveMenuBar extends JMenuBar {
	/**
	 * serialVersionUID = 1L.
	 */
	private static final long serialVersionUID = 1L;

	private ApplicationController applicationController;

	public TrackMoveMenuBar(ApplicationController applicationController) {
		this.applicationController = applicationController;
		createFileMenu();
		createAdminMenu();
		createGCMenu();
		createToolsMenu();
	}

	private void createFileMenu() {
		JMenu menu = new JMenu("File");
		add(menu);
		NMenuItem menuItem = new NMenuItem("Login", "doLogin", this);
		menu.add(menuItem);
		menuItem = new NMenuItem("Logout", "doLogout", this);
		menu.add(menuItem);
		menu.addSeparator();
		menuItem = new NMenuItem("Exit", "doExit", this);
		menu.add(menuItem);
	}

	private void createAdminMenu() {
		JMenu menu = new JMenu("Admin");
		add(menu);
		NMenuItem menuItem = new NMenuItem("Manage User", "doUser", this);
		menu.add(menuItem);
		menuItem = new NMenuItem("Manage Party", "doParty", this);
		menu.add(menuItem);
		menuItem = new NMenuItem("Manage Vehicle", "doVehicle", this);
		menu.add(menuItem);
		menuItem = new NMenuItem("Manage Account", "doAccount", this);
		menu.add(menuItem);
		/*
		 * menuItem = new NMenuItem("Logout", "doLogout", this);
		 * menu.add(menuItem); menu.addSeparator(); menuItem = new
		 * NMenuItem("Exit", "doExit", this); menu.add(menuItem);
		 */
	}

	private void createGCMenu() {
		JMenu menu = new JMenu("GC-Tools");
		add(menu);
		NMenuItem menuItem = new NMenuItem("Manage GC", "doGC", this);
		menu.add(menuItem);
		/*
		 * menuItem = new NMenuItem("Manage Party", "doParty", this);
		 * menu.add(menuItem); menuItem = new NMenuItem("Manage Vehicle",
		 * "doVehicle", this); menu.add(menuItem); menuItem = new
		 * NMenuItem("Manage Account", "doAccount", this); menu.add(menuItem);
		 */
		/*
		 * menuItem = new NMenuItem("Logout", "doLogout", this);
		 * menu.add(menuItem); menu.addSeparator(); menuItem = new
		 * NMenuItem("Exit", "doExit", this); menu.add(menuItem);
		 */
	}

	private void createToolsMenu() {
		JMenu menu = new JMenu("Tools");
		add(menu);
		NMenuItem menuItem = new NMenuItem("Search tool", "doSearch", this);
		menu.add(menuItem);
		/*
		 * menuItem = new NMenuItem("Manage Party", "doParty", this);
		 * menu.add(menuItem); menuItem = new NMenuItem("Manage Vehicle",
		 * "doVehicle", this); menu.add(menuItem); menuItem = new
		 * NMenuItem("Manage Account", "doAccount", this); menu.add(menuItem);
		 */
		/*
		 * menuItem = new NMenuItem("Logout", "doLogout", this);
		 * menu.add(menuItem); menu.addSeparator(); menuItem = new
		 * NMenuItem("Exit", "doExit", this); menu.add(menuItem);
		 */
	}

	public void doLogin() {
		this.setEnabled(false);
		LoginDialog dialog = new LoginDialog(applicationController);
		User user = dialog.login();
		if (user != null) {
			ApplicationContest applicationContest = new ApplicationContest(
					"Track Move");
			applicationContest.setUserName(user.getLogin());
			applicationContest.setUserType(user.getRole());
			applicationController.setApplicationContest(applicationContest);
			this.setEnabled(true);
		}

	}

	public void doLogout() {
		logout(false);
	}

	public void doExit() {
		logout(true);
	}

	private void logout(boolean exit) {
		if (exit
				&& MessageUtilities.showAsValidationDialog(new JLabel(
						"Are you sure, you want to Exit?"),
						MessageUtilities.WARNING, "TrackMove-Exit")) {
			applicationController.logout();
			System.exit(0);
		}
		if (!exit
				&& MessageUtilities.showAsValidationDialog(new JLabel(
						"Are you sure, you want to logout ?"),
						MessageUtilities.WARNING, "Logout")) {
			applicationController.logout();
			doLogin();
		}
	}

	public void doUser() {
		LoginViewController controller = new LoginViewController();
		applicationController.openScreen(controller, IconFactory
				.getImage(IconFactory.USER), new Dimension(650, 300));
	}

	public void doVehicle() {
		VehicleViewController controller = new VehicleViewController();
		applicationController.openScreen(controller, IconFactory
				.getImage(IconFactory.TRUCK_2), new Dimension(750, 500));
	}

	public void doParty() {
		PartyViewController controller = new PartyViewController();
		applicationController.openScreen(controller, IconFactory
				.getImage(IconFactory.CLIENT), new Dimension(800, 550));
	}

	public void doAccount() {
		AccountViewController controller = new AccountViewController();
		applicationController.openScreen(controller, IconFactory
				.getImage(IconFactory.ACCOUNT), new Dimension(800, 550));
	}

	public void doSearch() {
		/*
		 * SearchViewController<User> controller = new SearchViewController<User>(
		 * new UserSearchAction(), "Search user");
		 * applicationController.openScreen(controller, new Dimension(900,
		 * 500));
		 */
		SearchToolViewController controller = new SearchToolViewController();
		applicationController.openScreen(controller, IconFactory
				.getImage("magnifiengGlass.gif"), new Dimension(950, 650));
	}

	public void doGC() {
		GcViewController controller = new GcViewController();
		applicationController.openScreen(controller, IconFactory
				.getImage("magnifiengGlass.gif"), new Dimension(950, 650));

	}
}